<?php
/**
 * ============================================================================
 * Copyright (c) 2015-2020 贵州大师兄信息技术有限公司 All rights reserved.
 * siteַ: https://www.gzdsx.cn
 * ============================================================================
 * @author:     David Song<songdewei@163.com>
 * @version:    v1.0.0
 * ---------------------------------------------
 * Date: 2020/5/24
 * Time: 10:08 上午
 */

namespace App\Services\Wechat;


use App\Models\User;
use App\Models\UserConnect;
use App\Services\Contracts\WechatServiceInterface;
use App\Traits\WeChat\SyncWechatHeadImg;

abstract class WechatService implements WechatServiceInterface
{
    use SyncWechatHeadImg;

    protected $wechatUserInfo;

    public function __construct($wechatUserInfo = [])
    {
        $this->wechatUserInfo = collect($wechatUserInfo);
    }

    /**
     * @param string $appid
     * @param array $wechatUserInfo
     * @param string $openid
     * @param null $unionid
     * @return User|\Illuminate\Database\Eloquent\HigherOrderBuilderProxy|mixed
     */
    public function register($appid, $wechatUserInfo, $openid, $unionid = null)
    {
        // TODO: Implement register() method.
        $user = null;
        $this->wechatUserInfo = collect($wechatUserInfo);
        $connect = UserConnect::findByOpenid($openid);
        if ($connect) {
            $user = $connect->user;
        } else {
            if ($unionid) {
                $connect = UserConnect::findByUnionid($unionid);
                if ($connect) {
                    $user = $connect->user;
                }
            }
        }

        if (is_null($user)){
            $user = new User();
            $user->username = $this->getNickName();
            $user->state = 1;
        }
        $user->save();

        $connect = $user->connects()->firstOrNew(['openid' => $openid]);
        $connect->fill([
            'appid' => $appid,
            'unionid' => $unionid,
            'nickname' => $this->getNickName(),
            'avatar' => $this->getAvatar(),
            'gender' => $this->getGender(),
            'country' => $this->wechatUserInfo->get('country'),
            'province' => $this->wechatUserInfo->get('province'),
            'city' => $this->wechatUserInfo->get('city'),
            'platform' => $this->getPlatForm()
        ])->save();

        $user->info()->update([
            'gender' => $this->getGender(),
            'country' => $this->wechatUserInfo->get('country'),
            'province' => $this->wechatUserInfo->get('province'),
            'city' => $this->wechatUserInfo->get('city'),
        ]);

        if (!$user->avatar_state) {
            $this->downloadWechatHeadImg($user->uid, $this->getAvatar());
            $user->avatar_state = 1;
            $user->save();
        }

        return $user;
    }

    /**
     * @return mixed|string
     */
    protected function getNickName()
    {
        return $this->wechatUserInfo->get('nickname');
    }

    /**
     * @return mixed
     */
    protected function getAvatar()
    {
        return $this->wechatUserInfo->get('headimgurl');
    }

    /**
     * @return mixed
     */
    protected function getGender()
    {
        return $this->wechatUserInfo->get('sex');
    }

    /**
     * @return string
     */
    protected function getPlatForm()
    {
        return 'officialaccount';
    }
}
